package com.googlecode.horncomb.web.extremetable.custom;

import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.TableActions;
import org.extremecomponents.util.HtmlBuilder;

public class LinkToolbarBuilder extends
		org.extremecomponents.table.view.html.ToolbarBuilder {

	public LinkToolbarBuilder(TableModel model) {
		super(model);
	}

	public LinkToolbarBuilder(HtmlBuilder html, TableModel model) {
		super(html, model);
	}

	@Override
	public void rowsDisplayedDroplist() {
		int total = getTableModel().getLimit().getTotalRows();
		int rowsDisplayed = getTableModel().getTableHandler().getTable()
				.getRowsDisplayed();
		int medianRowsDisplayed = getTableModel().getTableHandler().getTable()
				.getMedianRowsDisplayed();
		int maxRowsDisplayed = getTableModel().getTableHandler().getTable()
				.getMaxRowsDisplayed();
		int currentRowsDisplayed = getTableModel().getLimit()
				.getCurrentRowsDisplayed();

		getHtmlBuilder().select().name(
				getTableModel().getTableHandler().prefixWithTableId()
						+ TableConstants.ROWS_DISPLAYED).styleClass(
				"rowsDisplayed");

		StringBuffer onchange = new StringBuffer();
		onchange.append(new TableActions(getTableModel())
				.getRowsDisplayedAction());
		getHtmlBuilder().onchange(onchange.toString());

		getHtmlBuilder().close();

		getHtmlBuilder().newline();
		getHtmlBuilder().tabs(4);

		// default rows
		getHtmlBuilder().option().value(String.valueOf(rowsDisplayed));
		if (currentRowsDisplayed == rowsDisplayed) {
			getHtmlBuilder().selected();
		}
		getHtmlBuilder().close();
		getHtmlBuilder().append(String.valueOf(rowsDisplayed));
		getHtmlBuilder().optionEnd();

		// median rows
		getHtmlBuilder().option().value(String.valueOf(medianRowsDisplayed));
		if (currentRowsDisplayed == medianRowsDisplayed) {
			getHtmlBuilder().selected();
		}
		getHtmlBuilder().close();
		getHtmlBuilder().append(String.valueOf(medianRowsDisplayed));
		getHtmlBuilder().optionEnd();

		// max rows
		getHtmlBuilder().option().value(String.valueOf(maxRowsDisplayed));
		if (currentRowsDisplayed == maxRowsDisplayed) {
			getHtmlBuilder().selected();
		}
		getHtmlBuilder().close();
		getHtmlBuilder().append(String.valueOf(maxRowsDisplayed));
		getHtmlBuilder().optionEnd();

		// all rows
		if ((Boolean) this.getTableModel().getTableHandler().getTable()
				.getAttribute(SimpleLinkView.STYLECLASS_SHOW_ALL_ROWS)) {
			getHtmlBuilder().option().value(String.valueOf(total));
			if (currentRowsDisplayed == total) {
				getHtmlBuilder().selected();
			}
			getHtmlBuilder().close();
			getHtmlBuilder().append(
					String.valueOf(getMessages().getMessage(
							"toolbar.text.allRows")));
			getHtmlBuilder().optionEnd();
		}

		getHtmlBuilder().newline();
		getHtmlBuilder().tabs(4);
		getHtmlBuilder().selectEnd();
	}
}
